﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace MyMusicCollection.Model
{
	class AlbumHandler
	{
		public Album GetAlbum(string aID)
		{
			Album theAlbum = new Album();

			DiscogsReader dcReader = new DiscogsReader();
			XmlTextReader xmlReader = dcReader.GetAlbumResponse(aID);
			XmlDocument xmlDoc = new XmlDocument();

			CollectionHandler colHand = new CollectionHandler();
			List<ArtistAlbum> colList = new List<ArtistAlbum>();
			colList = colHand.GetCollection();

			if (xmlReader == null)
			{
				Console.WriteLine("Fel vid kopplingen till Discogs.");
				return null;
			}

			try
			{
				xmlReader.Read();
				xmlDoc.LoadXml(xmlReader.ReadOuterXml());
				xmlReader.Close();

				XmlNodeList nList = xmlDoc.SelectNodes("resp");

				foreach (XmlNode node in nList)
				{
					//ID
					theAlbum.mID = int.Parse(node.SelectSingleNode("release").Attributes.GetNamedItem("id").InnerText);

					//Artist
					if (node.SelectSingleNode("release/artists/artist") != null)
					{
						theAlbum.mArtist = node.SelectSingleNode("release/artists/artist").InnerText;
					}
					else
					{
						theAlbum.mArtist = "artist_ej_tillgänglig";
					}

					//Title
					if (node.SelectSingleNode("release/title") != null)
					{
						theAlbum.mTitle = node.SelectSingleNode("release/title").InnerText;
					}
					else
					{
						theAlbum.mTitle = "titel_ej_tillgänglig";
					}

					//Bild-uri
					if (node.SelectSingleNode("release/images/image") != null)
					{
						if (node.SelectSingleNode("release/images/image").Attributes.GetNamedItem("uri") != null)
						{
							theAlbum.mImageUri = node.SelectSingleNode("release/images/image").Attributes.GetNamedItem("uri").InnerText;
						}
					}
					else
					{
						theAlbum.mImageUri = null;
					}

					//Releaseår
					if (node.SelectSingleNode("release/released") != null)
					{
						theAlbum.mYear = node.SelectSingleNode("release/released").InnerText;
					}
					else
					{
						theAlbum.mYear = "Okänt år";
					}

					//Format
					if (node.SelectSingleNode("release/formats/format") != null)
					{
						if (node.SelectSingleNode("release/formats/format").Attributes.GetNamedItem("name") != null)
						{
							theAlbum.mFormat = node.SelectSingleNode("release/formats/format").Attributes.GetNamedItem("name").InnerText;
						}
						else
						{
							theAlbum.mFormat = "Okänt format";
						}
					}
					else
					{
						theAlbum.mFormat = "Okänt format";
					}

					//Genre
					if (node.SelectSingleNode("release/genres/genre") != null)
					{
						theAlbum.mGenre = node.SelectSingleNode("release/genres/genre").InnerText;
					}
					else
					{
						theAlbum.mGenre = "Okänd genre";
					}

					//Label
					if (node.SelectSingleNode("release/labels/label") != null)
					{
						if (node.SelectSingleNode("release/labels/label").Attributes.GetNamedItem("name") != null)
						{
							theAlbum.mLabel = node.SelectSingleNode("release/labels/label").Attributes.GetNamedItem("name").InnerText;
						}
						else
						{
							theAlbum.mLabel = "Okänt skivbolag";
						}
					}
					else
					{
						theAlbum.mLabel = "Okänt skivbolag";
					}

					//Land
					if (node.SelectSingleNode("release/country") != null)
					{
						theAlbum.mCountry = node.SelectSingleNode("release/country").InnerText;
					}
					else
					{
						theAlbum.mCountry = "Okänt ursprung";
					}

					//Övrig info
					if (node.SelectSingleNode("release/notes") != null)
					{
						theAlbum.mNotes = node.SelectSingleNode("release/notes").InnerText;
					}
					else
					{
						theAlbum.mNotes = "Ingen övrig information tillgänglig.";
					}

					XmlNodeList trackList = xmlDoc.SelectNodes("resp/release/tracklist/track");

					foreach (XmlNode trackNode in trackList)
					{
						AlbumTrack track = new AlbumTrack();

						track.mArtist = theAlbum.mArtist;
						track.mAlbum = theAlbum.mTitle;
				
						//Position
						if (trackNode.SelectSingleNode("position") != null)
						{
							track.mPosition = trackNode.SelectSingleNode("position").InnerText;
						}
						else
						{
							track.mPosition = "0";
						}

						//Titel
						if (trackNode.SelectSingleNode("title") != null)
						{
							track.mTitle = trackNode.SelectSingleNode("title").InnerText;
						}
						else
						{
							track.mTitle = "Okänd titel";
						}

						//Längd
						if (trackNode.SelectSingleNode("duration") != null)
						{
							if (trackNode.SelectSingleNode("duration").InnerText != "")
							{
								track.mDuration = trackNode.SelectSingleNode("duration").InnerText;
							}
							else
							{
								track.mDuration = "xx:xx";
							}
						}
						else
						{
							track.mDuration = "xx:xx";
						}

						theAlbum.mTrackList.Add(track);
					}
				}

				//Används för att dölja lägg till knappen om albumet redan finns i samlingen
				if (colList != null)
				{
					foreach (ArtistAlbum a in colList)
					{
						if (theAlbum.mID == a.mID)
						{
							theAlbum.mInCollection = true;
						}
					}
				}

				return theAlbum;
			}
			catch (Exception e)
			{
				Console.WriteLine("Något gick fel med kopplingen till Discogs.");
				xmlReader.Close();
				return null;
			}
		}
	}
}
